package com.szt.sy.controller.blog.articletag;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.szt.sy.anontation.Authority;
import com.szt.sy.anontation.AuthorityConfig;
import com.szt.sy.common.AuthorityCommon;
import com.szt.sy.controller.base.AdminBaseController;
import com.szt.sy.entity.blog.ArticleTag;
import com.szt.sy.service.blog.articletag.ArticleTagService;
import com.szt.sy.util.date.DateUtil;
import com.szt.sy.util.security.SecurityUtils;
import com.szt.sy.vo.rep.base.RepBaseVO;

/***
 * 文章标签-controller
 * 
 * @author wwy
 *
 */
@Controller
@AuthorityConfig(prefix = "/admin/articletag/list")
@RequestMapping(value = "admin/articletag")
public class ArticleTagController extends AdminBaseController {
	
	@Autowired
	private ArticleTagService articletagService;
	
	/**
	 * 去列表页面
	 *
	 * @param @
	 */
	@Authority(name = AuthorityCommon.LIST)
	@GetMapping(value = "/list")
	public ModelAndView goPage(@RequestParam Map<String, Object> map) {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("blog/articletag/articletag_list");
		mv.addObject("queryParam", map);
		
		return mv;
	}
	
	/**
	 * 列表
	 *
	 * @param page
	 * @
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Authority(name = AuthorityCommon.LIST)
	@PostMapping(value = "/list")
	@ResponseBody
	public RepBaseVO list(Page<ArticleTag> page, @RequestParam Map<String, Object> map) {
		RepBaseVO repVO = new RepBaseVO<>();
		processParams(map);
		page = articletagService.list(new Page<ArticleTag>(page.getCurrent(), page.getSize()), map);
		List<ArticleTag> varList = page.getRecords();
		Map<String, Object> resultInfo = new HashMap<>(2);
		resultInfo.put("data", varList);
		resultInfo.put("total", page.getTotal());
		return repVO.setCommonSuccess().setData(resultInfo);
	}
	
	/***
	 * 处理输入参数
	 *
	 * @param map
	 */
	private void processParams(Map<String, Object> map) {
		String sStartDate = (String) map.get("lastStart");
		String sEndDate = (String) map.get("lastEnd");
		if (StringUtils.isBlank(sStartDate)) {
			map.put("lastStart", null);
		} else {
			map.put("lastStart", DateUtil.getStartTimeOfDate(sStartDate));
		}
		if (StringUtils.isBlank(sEndDate)) {
			map.put("lastEnd", null);
		} else {
			map.put("lastEnd", DateUtil.getEndTimeOfDate(sEndDate));
		}
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
	}
}
